package com.midcent.networklib.interfac;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.ext.tools.HttpTools;
import com.midcent.networklib.manager.HttpRequestNetManager;
import com.midcent.networklib.utils.logger.LogUtils;
import com.midcent.networklib.utils.network.NetUtils;

import com.midcent.networklib.R;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by xiangke on 2016/1/15.
 */
public class HttpBaseNoCachedRequest {

    protected Context mContext;
    protected String baseurl = HttpRequestNetManager.getServerHost();
    protected JSONObject postpara = new JSONObject();
    protected String paramaters = "";
    protected String url = null;
    boolean isform = false;
    protected String TAG = this.getClass().getName();
    protected RequestListener requestlistener;
    protected String timestamp;

    public HttpBaseNoCachedRequest(Context context) {
        this.mContext = context;
        this.timestamp = Long.toString(System.currentTimeMillis());
        init();
    }

    public HttpBaseNoCachedRequest(Context context, String purl) {
        this.mContext = context;
        this.timestamp = Long.toString(System.currentTimeMillis());
        this.url = purl;
    }

    public String getTimeStamp() {
        return this.timestamp;
    }

    public void setRequestListener(RequestListener prequestlistener) {
        this.requestlistener = prequestlistener;
    }

    public void setParam(String para) {
        if (!TextUtils.isEmpty(para)) {
            isform = true;
            paramaters = para;
        }
    }

    public void setPostBody(JSONObject para) {
        this.postpara = para;
    }

    protected String getContentBody() {
        if (!TextUtils.isEmpty(paramaters)) {
            return paramaters;
        }
        return postpara.toString();
    }

    public void post() {
        sendRequst(Request.Method.POST);
    }
    public void get() {
        sendRequst(Request.Method.GET);
    }

    public void sendRequst(int method) {
        if (!NetUtils.isConnected(mContext)) {
            if (requestlistener != null) {
                requestlistener.onError(mContext.getResources().getString(R.string.network_notavaliable));
            }
            onErrorReponse(mContext.getResources().getString(R.string.network_notavaliable), mContext.getResources().getString(R.string.network_notavaliable));
            return;
        }
        HttpTools httptool = new HttpTools(mContext);
        HttpJObjectRequest request = new HttpJObjectRequest(method, url, getContentBody(), isform, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                JSONObject resp = (JSONObject) response;
                LogUtils.w(TAG, "Request " + url + "postpara=" + getContentBody() + "---" + response.toString());
                if (resp.optInt("code") == 2000) {
                    //Login success
                    Log.e(url, resp.toString());
                    JSONObject data = resp.optJSONObject("data");
                    if (data != null) {
                        onSuccessReponse(data);
                        if (requestlistener != null) {
                            requestlistener.onResultSuccess(data);
                        }

                    } else {
                        JSONArray datalist = resp.optJSONArray("data");
                        if (datalist != null) {
                            onSuccessReponse(data);
                            if (requestlistener != null) {
                                requestlistener.onResultSuccess(datalist);
                            }

                        }else {
                            onSuccessReponse(response);
                            if (requestlistener != null) {
                                requestlistener.onResultSuccess(response);
                            }

                        }


                    }
                } else {
                    onErrorReponse(response.toString(), response.toString());
                    if (requestlistener != null) {
                        requestlistener.onError(response);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LogUtils.e(TAG, "Request " + url + "postpara=" + getContentBody() + " errormsg" + error.toString());
                onErrorReponse(mContext.getResources().getString(R.string.network_connect_failed), mContext.getResources().getString(R.string.network_connect_failed));
                if (requestlistener != null) {
                    requestlistener.onError(mContext.getResources().getString(R.string.network_connect_failed));
                }
            }
        });
        request.setTimestamp(timestamp);
        httptool.sendRequest(request);
    }

    public void init() {
    }

    public void onSuccessReponse(Object response) {
    }

    public void onErrorReponse(String errormsg, String errtype) {
    }
}
